<template>
  <view>
    <!-- 购物车区域 -->
    <view class="cart-container" v-if="cart.length!==0">
      <!-- 收获地址区域 -->
      <my-address></my-address>
      <!-- 商品列表标题区域 -->
      <view class="cart-title">
        <uni-icons type="shop" size="18" color="gray"></uni-icons>
        <text class="cart-text">购物车</text>
      </view>
      <!-- 商品列表循环区域 -->
      <uni-swipe-action>
        <block v-for="(item,index) in cart" :key="index">
          <uni-swipe-action-item :right-options="options" @click="deleteGoods(item)">
            <my-goods :item="item" :showRadio="true" :showNum="true" @radio-change="radioChangeHandel"
              @number-change="numberChangeHandle" @goToDetailHandle="goToDetailHandle(item)"></my-goods>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>
      <!-- 商品结算区域 -->
      <my-settle></my-settle>
    </view>
    <!-- 空白购物车区域 -->
    <view class="null-shops" v-else>
      <image src="../../static/cart.png"></image>
      <text>空空如也~</text>
    </view>
  </view>
</template>

<script>
  import tabBarBadge from '@/mixins/tabBarBadge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [tabBarBadge],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000',
            color: '#fff'
          }
        }]
      };
    },
    computed: {
      ...mapState('m_cart', ['cart']),
    },
    methods: {
      ...mapMutations('m_cart', ['updateState', 'updateCount', 'deleteGoodsById']),
      // 单选框值改变处理函数
      radioChangeHandel(e) {
        // console.log(e)
        // 修改状态
        this.updateState(e)
      },
      // 技术器值改变处理函数
      numberChangeHandle(e) {
        // 存储商品数量
        this.updateCount(e)
      },
      // 删除商品
      deleteGoods(goods) {
        this.deleteGoodsById(goods.goods_id)
      },
      // 跳转至商品详情页
      goToDetailHandle(item) {
        console.log(1111)
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;
  }

  // 购物车标题
  .cart-title {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    height: 40px;
    padding-left: 5px;
    color: gray;
    border-bottom: 1px solid #efefef;

    .cart-text {
      font-size: 13px;
      margin-left: 10px;
    }
  }

  // 空购物车样式
  .null-shops {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 90vh;

    image {
      width: 200rpx;
      height: 200rpx;
    }

    text {
      margin-top: 10px;
      color: #afafaf;
      font-size: 28rpx;
    }
  }
</style>
